/*
 * Created on 2007-12-29
 *
 * TODO 
 */
package jacky.lanlan.song.extension.struts.action;

import jacky.lanlan.song.TestDomain;
import jacky.lanlan.song.extension.struts.Scope;
import jacky.lanlan.song.extension.struts.annotation.*;
import jacky.lanlan.song.extension.struts.resource.Downloadable;
import jacky.lanlan.song.extension.struts.resource.FileResource;
import jacky.lanlan.song.extension.struts.util.Assert;
import jacky.lanlan.song.extension.struts.util.ResourceUtils;

import java.util.Calendar;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@POJOAction
public class POJOTestAction{

	@Execution
	@Form(type=TestFormBean.class,validate=true)
	@Input("/login.jsp")
	public String formValidateError(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.isTrue(false, "永远不可能运行到这里！！！");
		return Forward.SUCCESS;
	}
	
	@Execution
	@Form
	@Input("/login.jsp")
	@Forward(name={"success"}, 
			path={"/BBB.jsp"},
			redirect= {false})
	@SuccessTo(path = "/successTo.jsp")//覆盖上面的Forward
	public String businessMethod(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.isTrue(input.get("aaa").equals("string value"));
		Assert.isTrue(Integer.parseInt(input.get("bbb"))==111);
		
		Assert.isTrue(vs.getRequestParameter("param").equals("value_reqParam"));

		vs.setAttribute("ccc", "out_request",Scope.Request);
		vs.setAttribute("ddd", "out_session",Scope.Session);
		
		Assert.isTrue(vs instanceof ActionContext);
		ActionContext ac=(ActionContext) vs;
		
		Object value=ac.getContext();
		Assert.notNull(value);
		Assert.isTrue(ServletContext.class.isAssignableFrom(value.getClass()));
		
		value=ac.getRequest();
		Assert.notNull(value);
		Assert.isTrue(HttpServletRequest.class.isAssignableFrom(value.getClass()));

		value=ac.getResponse();
		Assert.notNull(value);
		Assert.isTrue(HttpServletResponse.class.isAssignableFrom(value.getClass()));
		
		return Forward.SUCCESS;
	}
	
	//注意这个取消方法对应businessMethod，执行Cancell后的目标页面将在businessMethod标注中
	//寻找，所以这里的目标页面是/login.jsp
	public String businessMethod_Cancelled(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.isTrue(input.size()==0, "应该是一个空Map");
		return Forward.INPUT; 
	}
	
	@Execution
	@Input("/login.jsp")
	@Forward(name={"success"}, 
			path={"/AAA.jsp"},
			redirect= {false})
	@FailureTo(path="/failureTo.jsp")
	public String anotherBusinessMethod(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.isTrue(input.get("aaa").equals("string value2"));
		Assert.isTrue(Integer.parseInt(input.get("bbb"))==222);
		Assert.isTrue(vs.getRequestParameter("param").equals("value_reqParam2"));

		vs.setAttribute("ccc", "out_request2",Scope.Request);
		vs.setAttribute("ddd", "out_session2",Scope.Session);
		
		Assert.isTrue(vs instanceof ActionContext);
		ActionContext ac=(ActionContext) vs;
		
		Object value=ac.getContext();
		Assert.notNull(value);
		Assert.isTrue(ServletContext.class.isAssignableFrom(value.getClass()));
		
		value=ac.getRequest();
		Assert.notNull(value);
		Assert.isTrue(HttpServletRequest.class.isAssignableFrom(value.getClass()));

		value=ac.getResponse();
		Assert.notNull(value);
		Assert.isTrue(HttpServletResponse.class.isAssignableFrom(value.getClass()));
		
		return Forward.FAILURE;
	}

	@Execution
	@Domain(TestDomain.class)
	@Form()
	@Input("/login.jsp")
	@SuccessTo(path="/AAA.jsp")
	@FailureTo(path="/failureTo.jsp")
	public String executeMethodWithDomain(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.notNull(vs.getAttribute("domain"));
		Assert.isTrue(vs.getAttribute("domain").getClass()==TestDomain.class);
		
		TestDomain domain=(TestDomain) vs.getAttribute("domain");
		Assert.isTrue(domain.getInstallNum()==111);
		Assert.isTrue(domain.getCarNo().equals("川A8888"));
		
		Assert.notNull(domain.getEmitTime());
		Calendar c=Calendar.getInstance();
		c.setTime(domain.getEmitTime());
		Assert.isTrue(c.get(Calendar.YEAR)==2008);
		Assert.isTrue(c.get(Calendar.MONTH)==1);
		Assert.isTrue(c.get(Calendar.DATE)==11);
		
		return Forward.SUCCESS;
	}
	
	@Execution
	@Form
	@Input("/login.jsp")
	@Upload(factory=TestPersistenceFactory.class)
	public String upload(ValueStack vs,Map<String,String> input ) throws Exception {
		return Forward.INPUT;
	}

	@Download
	@Input("log4j_properties_download.jsp")
	public Downloadable prepare(ValueStack vs, Map<String, String> input)
			throws Exception {
		return new FileResource("application/properties",
				ResourceUtils.getFile("classpath:log4j.properties"),
				"log4j.properties");//模拟下载log4j配置文件
	}
	
	
	
}
